/**********************************************************************
*  Copyright (c) 2007 - Victor Jacobs - victor.jacobs@gmail.com
*
*  Permission is hereby granted, free of charge, to any person
*  obtaining a copy of this software and associated documentation
*  files (the "Software"), to deal in the Software without
*  restriction, including without limitation the rights to use,
*  copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the
*  Software is furnished to do so, subject to the following
*  conditions:
*
*  The above copyright notice and this permission notice shall be
*  included in all copies or substantial portions of the Software.
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*  OTHER DEALINGS IN THE SOFTWARE.
**********************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using Tao.Ode;
using Tao.OpenGl;

namespace Vj.OdeObjectLibrary
{
    public class Ball //: WalkingApp.IObject
    {
        /*
         * Can add a density property, and resolution 
         * 
         **/

        #region Properties

        protected IntPtr body;
        protected IntPtr geom;
        protected Ode.dVector3 position;
        protected Ode.dVector3 color;
        protected Ode.dVector3 velocity;
        protected float mass;
        private float radius = 1.0f;

        public IntPtr Body
        {
            get { return body; }
            set { body = value; }
        }

        public IntPtr Geometry
        {
            get { return geom; }
            set { geom = value; }
        }

        public float Mass
        {
            get { return mass; }
            //set { mass = value; }
        }

        public Ode.dVector3 Color
        {
            get { return color; }
            //set { color = value; }       
        }

        public Ode.dVector3 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Ode.dVector3 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public float Radius
        {
            get { return radius; }
            //set { radius = value; }
        }

        #endregion

        #region Constructors

        public Ball(IntPtr world, IntPtr space, float size, float mass, Ode.dVector3 position, Ode.dVector3 force, Ode.dVector3 color)
        {
            this.body = Ode.dBodyCreate(world);

            this.position = position;
            this.color = color;
            this.radius = size;

            Ode.dBodySetPosition(body, position.X, position.Y, position.Z);
            Ode.dBodyAddForce(this.body, force.X, force.Y, force.Z);

            Ode.dMass objMass = new Ode.dMass();
            Ode.dMassSetSphere(ref objMass, 2500, size);
            objMass.mass = mass;
            Ode.dBodySetMass(this.body, ref objMass);

            this.geom = Ode.dCreateSphere(space, radius);
            Ode.dGeomSetBody(this.geom, body);
        }

        #endregion

        public void Render()
        {
            position = Ode.dBodyGetPosition(body);
            velocity = Ode.dBodyGetLinearVel(body);

            float[] mat_diffuse = { (float)color.X, (float)color.Y, (float)color.Z, 1.0f };
            float[] mat_specular ={ 0.9f * (float)color.X, 0.9f * (float)color.Y, 0.9f * (float)color.Z, 1.0f };
            float[] mat_ambient = { 0.5f * (float)color.X, 0.5f * (float)color.Y, 0.5f * (float)color.Z, 1.0f };

            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_SPECULAR, mat_specular);
            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_DIFFUSE, mat_diffuse);
            Gl.glMaterialfv(Gl.GL_FRONT, Gl.GL_AMBIENT, mat_ambient);

            Gl.glPushMatrix();

            Gl.glTranslated(position.X, position.Y, position.Z);

            Glu.gluSphere(Glu.gluNewQuadric(), (double)radius, 12, 12);

            Gl.glPopMatrix();
        }



    }
}
